﻿#pragma warning disable 1591
namespace Manga.ViewModels
{
    using Api.CCTuku;
    using Core;
    using Core.Models;
    using System;
    using System.Collections.ObjectModel;
    using System.Threading.Tasks;
    using Windows.UI.Popups;

    public class SearchViewModel:ViewModelBase
    {
        public SearchViewModel()
        {
            _Service = ServiceCollection[0];
        }

        private string _SearchText;
        public string SearchText {
            get { return _SearchText; }
            set { _SearchText = value;Update(); }
        }

        public ObservableCollection<IContentService> ServiceCollection { get; set; } =
            new ObservableCollection<IContentService>(){ new CCGallery() };

        public ObservableCollection<IComicModel> History { get; set; } =
            new ObservableCollection<IComicModel>();

        private IContentService _Service;
        public IContentService Service {
            get { return _Service; }
            set { _Service = value; Update(); }
        }

        private ObservableCollection<IComicModel> _SearchResult;
        public ObservableCollection<IComicModel> SearchResult {
            get { return _SearchResult; }
            set { _SearchResult = value;Update(); }
        }


        async public Task Search()
        {
            if (string.IsNullOrEmpty(SearchText)) {
                MessageDialog msg = new MessageDialog("请输入搜索内容");
                await msg.ShowAsync();
            }

            var result = await Service.Search(SearchText);
            if (result.ContainResult)
            {
                if (SearchResult == null) SearchResult = new ObservableCollection<IComicModel>();
                foreach(var i in result.Result)
                {
                    SearchResult.Add(i);
                }
            }
            else
            {

            }
        }
    }
}
